简体   繁体   中英

Jquery set variable input field value

My HTML:

<input id="penge" />
<input type="button" onclick="javascript: paymentwindow.open()" value="BETAL" id="betal" />

My jQuery:

<script type="text/javascript">
$(document).ready(function(){
var penge = $("#penge").val();


            paymentwindow = new PaymentWindow({
                         'amount': penge,
                         'currency': "DKK",
                         'language': "1",
                         'orderid': "155",
                         'callbackurl': "http://localhost:3000/"
            });
});
</script>

The variable is undefined. I want to set the variable penge when the betal button is clicked.

UPDATE:

<script type="text/javascript">
$(document).ready(function(){
var penge;
$('#betal').click(function(){
    penge = $("#penge").val();
    paymentwindow.open();
});




            paymentwindow = new PaymentWindow({
                         'amount': penge,
                         'currency': "DKK",
                         'language': "1",
                         'orderid': "155",
                         'callbackurl': "http://localhost:3000/"
            });
});
</script>

Penge is undefined. I have also removed the onclick js for the betal button.

var penge;
$('#betal').click(function(){
    var penge = $("#penge").val();
});

DEMO

Currently input field value is empty.

Enter some amount in the input field then click on betal button.

You will get an alert with the value entered on input field.

Do your stuff with the penge variable which is stored with your value.

var penge;
$('#betal').click(function(){     
    penge = $("#penge").val();     
    alert(penge);
}); 

HTML:

<input id="penge" /> 
<input type="button" value="BETAL" id="betal" />

Refer this LIVE DEMO

UPDATED:

I got your issue. Call the paymentwindow.open(); after the payment method declaration.

var penge;
var paymentwindow;
$('#betal').click(function(){
    var penge = $("#penge").val();
    paymentwindow = new PaymentWindow({
        amount: penge,
        currency: "DKK",
        language: "1",
        orderid: "155",
        callbackurl: 'http://localhost:3000/'
    });
    paymentwindow.open();    
});

Refer this LIVE DEMO 2

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