I want to toggle the value of a hidden input, between 开发者_Python百科advanced and normal? But the below script doesn't work, nothing happens.
$('#advancedLink').click(function () {
$("#searchType").val($(this).text() == 'normal' ? 'advanced' : 'normal');
return false;
});
However, if I switch place of normal and advanced it will work one time, but it won't switch back to normal if I click again.
$('#advancedLink').click(function () {
$("#searchType").val($(this).text() == 'normal' ? 'normal' : 'advanced');
return false;
});
But this last way seems rather unintuitive to me. Any ideas?
EDIT
This is the rendered HTML<div>
<div class="left" id="searchControl">
<a id="advancedLink" href="#">Advanced search</a><br>
</div>
<input type="hidden" value="normal" name="searchType" id="searchType">
</div>
You're checking the value of the link but setting the value for the text box. Since the value of the link never changes, the text box value does not change either.
Here's a fiddle : http://jsfiddle.net/GKfWB/
$('#advancedLink').click(function () {
$("#searchType").val($('#searchType').val() == 'normal' ? 'advanced' : 'normal');
console.log($("#searchType").val())
return false;
});
try to run:
$('#advancedLink').click(function () {
console.log($(this).text());
return false;
});
to see what is going on. probably you'll have to change .text() to .val()
Not sure if this is even what you're trying to do without some example html. (Try setting up a jsfiddle with your problem)
Here's an attempt to fix the possible problem: http://jsfiddle.net/infernalbadger/JF4tg/
$('#advancedLink').click(function () {
$("#searchType").val(function() {
return $(this).val() == 'normal' ? 'advanced' : 'normal';
});
return false;
});
精彩评论