开发者

Getting values from two different Inputs

开发者 https://www.devze.com 2023-03-31 11:19 出处:网络
<input class="lf" id="r3-1" type="text"/> <input class="rf" id="r4-1" type="text"/> <br>
<input class="lf" id="r3-1" type="text"/> 
<input class="rf" id="r4-1" type="text"/> <br>

<input class="lf" id="r3-2" type="text"/> 
<input class="rf" id="r4-2" type=&qu开发者_开发技巧ot;text"/> <br>
...

How to add values from this inputs into var code=''; like this?

ABC "value r3-1" "value r4-1"

ABC "value r3-2" "value r4-2"


Check with this below code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>        
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>

<title>Getting values ​​from two different Inputs</title>
</head>
<body>
<div id="inputs">
<input class="lf" id="r3-1" type="text" value="2"/> 
<input class="rf" id="r4-1" type="text" value="3"/> <br/>

<input class="lf" id="r3-2" type="text"/> 
<input class="rf" id="r4-2" type="text"/> <br/>

<input class="lf" id="r3-3" type="text"/> 
<input class="rf" id="r4-3" type="text"/> <br/>


<input class="lf" id="r3-4" type="text"/> 
<input class="rf" id="r4-4" type="text"/> <br/>
</div>
<input id="submit" type="button" value="Test" />
<div id="OutPut">
</div>

<script type="text/javascript">

    jQuery("#submit").click(function() {
    var temp, output="";
    jQuery('#inputs .lf').each(function(index) {
    /*we can also use .rf or br
    //jQuery('#inputs .rf').each(function(index) {
    //jQuery('#inputs br').each(function(index) {
    */
            temp = "ABC ";
            jQuery('#inputs input[id$=-' + (index + 1) + ']').each(function() { temp += '"' + this.value + '" '; })            
            output += temp + "<br/>";
        });
        jQuery("#OutPut").html(output);
    });

</script>
</body>
</html>

Output:-

Getting values from two different Inputs


You can access the value-property of your inputs like this:

$('#r3-1').val()

Then it's just a matter of string-formatting to get your var code the way you want it. I don't understand what you mean about

ABC "value r3-1" "value r4-1"

ABC "value r3-2" "value r4-2"



var val1 = jQuery('r3-1').val();
var val2 = jQuery('r3-2').val();
var code = val1 + val2;


I didnt clearly understand your question but if you want to get values of inputs u can use this.

var val1 = $('#r3-1').val()
var val2 = $('#r3-2').val()


You need to use JavaScript.

<script>
var code = '';
var r3_1 = document.getElementById("r3-1").value;
var r3_2 = document.getElementById("r3-2").value;

var r4_1 = document.getElementById("r4-1").value;
var r4_2 = document.getElementById("r4-2").value;

code = "ABC" + r3_1 + r4_1;
</script>


To get the value from a text input, the syntax is something like this:

$('#r3-1').val();

Rinse and repeat to get the other three values to build your output.

0

精彩评论

暂无评论...
验证码 换一张
取 消