Hands up - I can't figure it out what's wrong with it. Is that a bug or a wrong code ?
$(document).ready(function() {
$("#rem_but").click(function(){
var mail_name = $("#mail_rem").val();
var dataString = 'mail_name='+ mail_name;
if (mail_name.val() == "") { $("#rem_but").attr("disabled",true); }
else { $("#rem_but").removeAttr("disabled"); };
}); });
So when there's no input the button returns false correctly - when there's an input in the field - still the button returns false, hence the removeAttr() doesn't work - why ? Regards.
Hands up - I can't figure it out what's wrong with it. Is that a bug or a wrong code ?
$(document).ready(function() {
$("#rem_but").click(function(){
var mail_name = $("#mail_rem").val();
var dataString = 'mail_name='+ mail_name;
if (mail_name.val() == "") { $("#rem_but").attr("disabled",true); }
else { $("#rem_but").removeAttr("disabled"); };
}); });
So when there's no input the button returns false correctly - when there's an input in the field - still the button returns false, hence the removeAttr() doesn't work - why ? Regards.
Share Improve this question edited Sep 19, 2011 at 18:43 Arnaud Le Blanc 100k24 gold badges211 silver badges196 bronze badges asked Sep 19, 2011 at 18:40 Rod Rig GezRod Rig Gez 1332 silver badges11 bronze badges 3- You should be getting JavaScript errors in the console. – epascarello Commented Sep 19, 2011 at 18:44
- rem_but is not a submit button right? Rather its just a regular button? – Kris Krause Commented Sep 19, 2011 at 19:18
- @Kris - yes it's a 'button' type...<input type="text" maxlength="50" id="mail_rem" name="mail_rem" /><br/><br/> <input type="button" id="rem_but" name="rem_but" value="submit"/> – Rod Rig Gez Commented Sep 19, 2011 at 19:24
3 Answers
Reset to default 5try (mail_name.val() == "")
change to (mail_name == "")
Are you using jQuery 1.6.x?
If so then you should try using the .prop()
function. See below:
Disable/enable an input with jQuery?
Also, in your if statement no need to keep selecting $("#rem_but")
. Based on your code I would remend $(this)
instead -
$(this).prop('disabled', true);
This should work -
$(document).ready(function() {
$("#rem_but").click(function(e) {
e.preventDefault();
var mail_name = $.trim($("#mail_rem").val());
var dataString = 'mail_name='+ mail_name;
if (mail_name === "") {
$(this).prop("disabled", true); }
else {
$(this).prop("disabled", false); }
});
});
Here is the working jsFiddle code -
http://jsfiddle/4rPc5/
Updated code -
http://jsfiddle/4rPc5/2/
Perhaps you need to set the disabled attribute to 'false'?
if (mail_name.val() == "") { $("#rem_but").attr("disabled",true); }
else { $("#rem_but").attr("disabled",false); };
}
Or set it to an empty string
if (mail_name.val() == "") { $("#rem_but").attr("disabled",true); }
else { $("#rem_but").attr("disabled",""); };
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745088956a4610572.html
评论列表(0条)