javascript stops working when switching from text

2019-08-30 07:16发布

问题:

I'm trying to do javascript, but I'm a ruby guy and really suck with javascript. I have a one field form (text field and submit button) in which javascript only allows the form to be submitted if there is text in the field. However, I switched the field type from a text field to a select. Now, the form can't submit. I am almost certain the problem is with my javascript. Here is the code I have that word with a text field. How do I get it to work with the select?

Form:

<form id="new_skill" class="new_skill" method="post" action="/skills" >
    <li>
        <input id="resume-field" class="field field288" type="text"
          value="Type a speciality you want to add to your profile" 
          title="Type a speciality you want to add to your profile" 
          name="skill[label]"></input>
    </li>
    <li>
        <input class="button button-add button-add-disabled" 
         type="submit" value="ADD +" name="commit"></input>
    </li>
</form>

Javascript:

(function($){
    $(document).on('focusin', '#resume-field', function() {
        $(this).parents().find('.button-add-disabled').removeClass('button-add-disabled');
    }).on('focusout', '#resume-field', function(){
        if(this.value==' '||this.title==this.value) {
            $(this).parents().find('.button-add').addClass('button-add-disabled');
        } else {
            $(this).parents().find('.button-add').removeClass('button-add-disabled');
        }

    });     

    $('.button-add-disabled').click(function(){
        return !$(this).hasClass('button-add-disabled');
    });
}(jQuery));

css:

.button-add { width: 49px; height: 28px; border: solid 1px #8c8c8c; display: block; 
   font-size: 11px; line-height: 28px ; color: #fff; text-align: center; 
   font-family: 'Ubuntu', sans-serif; transition: none; margin: 0 0 0 auto; 
   border-radius: 3px; }
.button-add:hover { text-decoration: none;
   -webkit-transition:none; 
      -moz-transition:none; 
       -ms-transition:none; 
        -o-transition:none; 
           transition:none; 
}
.button-add-disabled { background: url(/assets/add-specialities-disabled.png) 
   repeat-x 0 0; box-shadow: 0 0 0 0; margin-left:35px;  }
.button-add-disabled:hover { background: url(/assets/add-specialities-disabled.png) 
   repeat-x 0 0; box-shadow: 0 0 0 0;  }

Html for form with drop down:

<select id="resume-field" name="skill[label]">
    <option value="1" title="option_1"></option>
    <option value="2" title="option 2" selected="selected"></option>
</select>

回答1:

I made a minimal example (jsfiddle), doing what I think you asked for, but not using your code. You can probably adapt it if it does what you want.

HTML

<form>
    <select id="selectme">
        <option value="">-- Please select one --</option>
        <option value="1">1</option>
        <option value="2">Two</option>
        <option value="3">III</option>
    </select> 
    <input type="submit" value="submit" id="clickme">
</form>

JS

$(function(){
    $("#clickme").prop("disabled", true);
    $("#selectme").change(function(){
        if ($("#selectme").val() != "") {
            $("#clickme").removeAttr("disabled");
        } else {
            $("#clickme").prop("disabled", true);
        }
    });
})

CSS

#clickme {
    background-color: #f00;
}

#clickme:disabled {
    background-color: #eee;
}


回答2:

you stated that you have switched the input field type from text to select. Therefore you need to add this kind of html:

<select>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select> 


回答3:

To start, your HTML is a little bit off:

<form id="new_skill" class="new_skill" method="post" action="/skills" >
    <!-- needs a parent UL element -->
    <li>
        <input id="resume-field" class="field field288" type="text"
          value="Type a speciality you want to add to your profile" 
          title="Type a speciality you want to add to your profile" 
          name="skill[label]"></input>
          <!-- as a side not, input tags can be self-close (<input type="text" />)
    </li>
    <li>
        <input class="button button-add button-add-disabled" 
         type="submit" value="ADD +" name="commit"></input>
    </li>
</form>

If you've changed your input to a select element, then you need option elements, of which each can have a "value" attribute, as follows:

<select>
    <option value="something">Something</option>
</select>

In the context of what you are trying to do, I'm not sure that a select element would fit your needs if you are wanting the user to type something in... You could pre-fill the select element with option elements, each corresponding to a specific "specialty that they want to add to their profile".



回答4:

UPDATED and SIMPLIFIED:

Assuming that you did change the <input> elements to <select><option></select> elements, your code will work fine with only minor changes.

jsFiddle Demo

Really, you only need to track the change event:

HTML:

<form id="new_skill" class="new_skill" method="post" action="/skills" >
    <li>
        Select a specialty to add to your profile:<br>
        <select id="resume-field" name="skill[label]">
            <option value="default">Select a speciality:</option>
            <option value="cod3r">Programming</option>
            <option value="pizza">Pizza consumption</option>
        </select>
    </li>
    <li>
        <input class="button button-add button-add-disabled" 
         type="submit" value="ADD +" name="commit"></input>
    </li>
</form>

javascript/jQuery:

$(document).on('change', '#resume-field', function() {
    if(this.value=='default') {
        $(this).parents().find('.button-add').addClass('button-add-disabled');
    } else {
        $(this).parents().find('.button-add').removeClass('button-add-disabled');
    }

});     

PS: Of course, to make the jsFiddle work I also had to temporarily remove the url() etc from your css...