将javascript(jquery)变量插入html输入字段(文本框) [英] Insert javascript (jquery) variable in html input field (textbox)

查看:136
本文介绍了将javascript(jquery)变量插入html输入字段(文本框)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用spandiv可以正常工作

$('#exchange_rate1').html(data[0].FinalCurrencyRate);

HTML

<span id="exchange_rate1"></span>

但是,如果HTML是<input type='text' name='exchange_rate1' id='exchange_rate1' value='<?php echo $post_exchange_rate; ?>>之类的输入元素,则什么也不会发生.

But if the HTML is an input element such as <input type='text' name='exchange_rate1' id='exchange_rate1' value='<?php echo $post_exchange_rate; ?>> then nothing happens.

value中删除了php代码相同.

我也尝试过document.getElementById("exchange_rate1").html(data[0].FinalCurrencyRate);,但我也看不到任何东西.

I also tried document.getElementById("exchange_rate1").html(data[0].FinalCurrencyRate); but I also see nothing.

现在清除,那需要使用val.我刚刚在Google搜索了如何在输入字段中插入jquery变量.找不到.

Now clear, that need to use val. I just searched google for how to insert jquery variable in input field. Could not find.

推荐答案

更具体地说:

// store the value you're looking to assign
var data = [
  { FinalCurrencyRate: <?= $post_echange_rate; ?> }
];

jQuery方式:

$('#exchange_rate1')               // grab the <input>
  .val(data[0].FinalCurrencyRate); // and assign it from the variable

直接js方式:

// normal JS version:
document.getElementById('exchange_rate1') // grab the <input>
  .value = data[0].FinalCurrencyRate;     // assign it

任何类型的表单字段(<input><select><textarea>)都使用 .val() 进行获取/设置,因为它们不包含子元素. .html() 应该用于结构元素.

Any kind of form fields (<input>,<select>,<textarea>) use .val() to get/set since they don't contain child elements. .html() should be used for structural elements.

这篇关于将javascript(jquery)变量插入html输入字段(文本框)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆