Jquery 如果输入有值,则替换内容
当一个隐藏的输入有一个包含字母“LO”的值时,我想用一些不同的html替换表单中的一个按钮 HTML:Jquery 如果输入有值,则替换内容,jquery,Jquery,当一个隐藏的输入有一个包含字母“LO”的值时,我想用一些不同的html替换表单中的一个按钮 HTML: 在上面的示例中,隐藏字段中有“LO”,那么我如何用替换按钮呢 <a class="button green" href="some.html">More information</a> 我尝试的是: if ($( "input[value*='LO']" ).find('.button').replaceWith('<a class="button gr
在上面的示例中,隐藏字段中有“LO”,那么我如何用替换按钮呢
<a class="button green" href="some.html">More information</a>
我尝试的是:
if ($( "input[value*='LO']" ).find('.button').replaceWith('<a class="button green" href="some.html">More information</a>'));
if($(“输入[value*='LO']”)。查找('.button')。替换为('');
但这不起作用,因为它还替换了隐藏字段中没有“LO”的按钮
非常感谢您的帮助
更新
我更新了代码,因为还有另一个输入字段,我没有提到 .find('.button')
到。next('.button')
.find()
=查找此元素中的元素
.next()
=查找此元素旁边的元素
使用
$(“输入[value^='LO'])
(输入以LO
开头)
您不需要使用
if-else语句
添加
:隐藏以获得更多焦点
或者使用您的输入idhiddenCode
(如果您确定它是唯一的id)
最后:
var newButton = '<a class="button green" href="some.html">More information</a>';
$("input:hidden[value^='LO']").next('.button').replaceWith(newButton);
var newButton='';
$(“输入:隐藏[value^='LO']”)。下一步('.button')。替换为(newButton);
使用ID
var newButton = '<a class="button green" href="some.html">More information</a>';
$("#hiddenCode").next('.button').replaceWith(newButton);
var newButton='';
$(“#隐藏代码”).next(“.button”).replacetwith(newButton);
使用类似于:
var v = $('#hiddenCode').val();
if (v && v.indexOf('LO') > -1) {
$('.button').replaceWith('<a class="button green" href="some.html">More information</a>');
}
var v=$('hiddenCode').val();
if(v&&v.indexOf('LO')>-1){
$('.button')。替换为('');
}
在:if($(“输入[value*='LO'])中不需要if语句
您需要下一步而不是查找:
$( "input[value^='LO']" ).next('.button').replaceWith('<a class="button green" href="some.html">More information</a>');
$(“输入[value^='LO']”)。下一步('.button')。替换为('');
Next搜索下一个兄弟,查找下一代。您根本不需要if
语句——对于您快速有效的解释来说,这是一个no-op.Thx。不幸的是,您的代码不起作用。我忘了在按钮之前还有另一个输入字段。因此“Next”不起作用???.Next().Next()(“.button”)
?@JaapVermoolen
$( "input[value^='LO']" ).next('.button').replaceWith('<a class="button green" href="some.html">More information</a>');