jQuery only gets the default value of input
I have two click events and two buttons. One button is hidden and one input is hidden.
When I click the button that is not hidden, the hidden button and the hidden input appear. When I click the second button, the one that was hidden, I get the default value of the input. This is the problem, it's getting the default value, and by default I mean whatever I typed in the value='dasad'
in the code. It's not getting the value that the user typed.
Some example code, don't look the syntax just as an example
$(button1).click(funcit开发者_StackOverflowon(){
$(button2).show();
$(input2).show();
})
$(button2).click(function(){
$(input2).val();
})
I tried using delegate
, but it's the same as the click
result.
I tried the following and was able to get it to work
$(document).ready(function() {
$('#button1').click(function(){
$('#button2').show();
$('#input2').show();
});
$('#button2').click(function(){
alert($('#input2').val());
})
});
Buttons are setup like
<button id="button1">hello</button>
<button id="button2" style="display:none">hello 2</button>
<input id="input2" style="display:none" type="text" />
$('#button1').click(function(){
$('#button2').show();
$('#input2').show();
return false;
});
$('#button2').click(function(){
var value = $('#input2').val();
alert(value);
return false;
});
In this situation there's really no reason you MUST used delegate but from what I've read it really is never a bad idea.
Here is an example JSfiddle doing what I think you want, it's a little hard to tell from your question.
$(document).ready(function() {
$(".wrapper").delegate(".button1", "click", function() {
$(".innerDiv").show();
$(".input1").focus();
});
$(".wrapper").delegate(".button2", "click", function() {
$(".showVal").html($(".input1").val());
});
});
精彩评论