如果用户输入了路径,则返回拆分值,否则返回空白
我需要动态显示用户输入的路径的名称和拆分路径。 为此,我拆分了用户输入的路径并仅抓取其中的某些部分。 例如,如果用户输入路径为:
/content/mypath/myfolder/about/images/abc.jpg然后我正在显示images / abc.jpg。
但是,假设用户只输入名称而不输入路径,在这种情况下至少应该应用和显示名称。
但每次进入路径时它都会显示0。 我该如何解决?
$(document).ready(function(){ $('#getData').click(function(){ var name = $('#name').val(); var imgPath = $('#imgPath').val(); var newPath = imgPath.match(/images\/.*$/i); $('.container').append( $('').text(name), $('
').text(function(imgPath){ return newPath ? imgPath : $('#imgPath').val(); }) ); //console.log(slicedPath); }); });
Name: Image path:
这应该可以解决您的问题。
我只更改了你的return语句return newPath != null ? newPath : imgPath;
return newPath != null ? newPath : imgPath;
$(document).ready(function(){ $('#getData').click(function(){ var name = $('#name').val(); var imgPath = $('#imgPath').val(); var newPath = imgPath.match(/images\/.*$/i); $('.container').append( $('').text(name), $('
').text(function(imgPath){ return newPath != null ? newPath : $('#imgPath').val(); }) ); //console.log(slicedPath); }); });
Name: Image path: /content/mypath/myfolder/about/images/abc.jpg