简体   繁体   中英

Jquery toggle input value and button class

I have a PHP page that loads a form with pre-populated values from a MySQL database. Users are able to edit the form and update it with their desired data, this works.

I would like to offer them the ability to toggle one of the fields if it's not applicable . By doing so, the field will be populated with the text 'N/A' and the button class should change to 'btn-warning'. When clicked again the 'N/A' should be removed and the previous data re-inserted - is this possible?

So far I have it sort of working... when the button is clicked the input is populated with 'N/A' and the class changes, however it doesn't toggle, ie the previous data doesn't re-appear and the 'N/A' remains.

I have created a FIDDLE , any help is appreciated.

 $(document).ready(function() { $("#ipclear").click(function() { $("#ip").val('N/A'); $(this).toggleClass('btn-default').toggleClass('btn-warning'); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/js/bootstrap.js"></script> <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.css" rel="stylesheet" /> <div class="input-group"> <input type="text" name='ip' id="ip" class='form-control' value="1.2.3.4"> <div class="input-group-btn"> <button type="button" class="btn btn-default" id="ipclear">Not Applicable</button> </div> </div> 

Store the text in a variable before toggle and set it again during next click.

Here is a working example:

 $(document).ready(function() { var toggleText = $("#ip").val(); $("#ipclear").click(function() { if ($("#ip").val() != 'N/A') { toggleText = $("#ip").val(); $("#ip").val('N/A'); } else $("#ip").val(toggleText); $(this).toggleClass('btn-default').toggleClass('btn-warning'); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/js/bootstrap.js"></script> <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.css" rel="stylesheet" /> <div class="input-group"> <input type="text" name='ip' id="ip" class='form-control' value="1.2.3.4"> <div class="input-group-btn"> <button type="button" class="btn btn-default" id="ipclear">Not Applicable</button> </div> </div> 

You are not restoring the value of the input field in your code. Somewhere you need to remember the old value and restore it on second click.

I updated your JSFIDDLE with a possible solution: https://jsfiddle.net/Doogie/6pqugkg3/5/

Some more tips:

If you have more than one field, then you need to remember the original value of each of them. One way you could do that is via data- attributes on the input elemnts:

<input value="currentValue" data-orig="originalValueFromDB" id="ip" ....>

You can get the original value with JQuery just as any other attribute:

var origValue = $('#ip').attr('data-orig'); // origValue = "originalValueFromDB'

You need to assign your text value in a variable and reassign after toggle.

 $(document).ready(function() { window.ipVal = $("#ip").val(); $("#ipclear").click(function() { $(this).toggleClass('btn-default btn-warning'); if($(this).hasClass('btn-default')){ //OR $(this).hasClass('btn-default') $("#ip").val(window.ipVal); }else{ $("#ip").val('N/A'); } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/js/bootstrap.js"></script> <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.css" rel="stylesheet" /> <div class="input-group"> <input type="text" name='ip' id="ip" class='form-control' value="1.2.3.4"> <div class="input-group-btn"> <button type="button" class="btn btn-default" id="ipclear">Not Applicable</button> </div> </div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM