是否可以将div的动态值转换为隐藏输入的值?
我喜欢这个的innerhtml值:
print(image_file)
插入此处
<div class="totals-value" id="total"></div>
答案 0 :(得分:4)
let $input = $('input[name="grandtotal"]');
let $div = $('#total');
$input.val( $div.html() );
答案 1 :(得分:1)
是的,您可以更改隐藏输入的值。我的代码片段有两个按钮“从div插入值到输入”和“显示隐藏的输入”将为您提供一个选项来玩它:
const input = $('input[name="grandtotal"]');
const div = $('#total');
function insert() {
input.val(div.html());
}
function showInput() {
input.prop('type', 'text');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="totals-value" id="total">123</div>
to be inserted here
<input type="hidden" name="grandtotal" value="">
<button onclick="insert()">Insert value from div to input</button>
<button onclick="showInput()">Show hidden input</button>
答案 2 :(得分:1)
var theHiddenInput = $('input[name=grandtotal]');
var theDiv = $("#total");
theHiddenInput.val(theDiv.text());