用于选择/文本框操作的JavaScript

JavaScript for select/Textbox action

本文关键字:操作 JavaScript 文本 选择 用于      更新时间:2023-09-26

我发现了很多我想做的事情的例子。不幸的是,我无法让任何例子发挥作用:)

我需要一个文本框来更改为所选下拉列表的值。

我创造了一把小提琴。

我尝试使用的JS是

$(document).ready(function() {
    $("#goal option").filter(function() {
    return $(this).val() == $("#reason").val();
}).attr('selected', true);
$("#goal").live("change", function() {
    $("#reason").val($(this).find("option:selected").attr("value"));
});
});

https://jsfiddle.net/phpman13/q8h5kupj/

我试过使用FireBug,但它报告说.live无效。

有什么想法吗?

.live()已弃用,请改用.on()。它遵循相同的语法。

发件人:https://api.jquery.com/live/

从jQuery 1.7开始,.live()方法已被弃用。使用.on()附加事件处理程序。旧版本jQuery的用户应该优先使用.delete()而不是.live().

我不清楚你的确切要求。我知道您希望根据所选的下拉项更新文本。如果是要求。

$(document).ready(function() {
    $("#reason").val($(this).find("option:selected").text());
});
$("#goal").change(function() {
        $("#reason").val($(this).find("option:selected").text());
});

try

$(document).ready(function() {
       $("#goal option").filter(function() {
          return $(this).val() == $("#reason").val();
       }).attr('selected', true);
    $("#goal").on("change", function() {
    $("#reason").val($(this).find("option:selected").attr("value"));
});
});

试试这个

$(document).ready(function() {
       $("#goal option").filter(function() {
          return $(this).val() == $("#reason").val();
       }).attr('selected', true);
    $("#goal").on("change", function() {
    $("#reason").val($(this).find("option:selected").text());
});
});

谢谢大家。更改

.live

.do

工作。