javascript onchage选择并更新输入或文本对象

时间:2017-05-16 18:39:56

标签: javascript jquery html onchange

我想做一个选择(没有php或其他代码),当值发生变化时,我想把值放在另一个html标签中,比如输入字段。我看过w3学校,并且只有代码才能获得select标签的价值。以下是我尝试过的内容:

<body>

         <select name="selection" id="selection" onchange="update(this.value)">
              <option value="1">One</option>
              <option value="2">Two</option>
              <option value="3">Three</option>

          </select>

<input name="desc" type="text" id="desc" />

</body>
<script>
function update(val) {
    var x = document.getElementsByName("desc");
    x.value = val;
    $('desc').val(val);
 }
 </script>

那么,在输入文本中应该用select中的值更新?没有任何事情发生

5 个答案:

答案 0 :(得分:1)

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>

         <select name="selection" id="selection" onchange="update(this.value)">
              <option value="1">One</option>
              <option value="2">Two</option>
              <option value="3">Three</option>

          </select>

<input name="desc" type="text" id="desc" />

</body>
<script>
function update(val) {
    var x = document.getElementsByName("desc");
    x[0].value = val;
    $('desc').val(val);
 }
 </script>

document.getElementsByName 返回数组,你必须为x提供索引以提供值

答案 1 :(得分:0)

也许

import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;

/**
 * Android's way to initialize the game.
 */
 public class AndroidLauncher extends AndroidApplication {


private AdView adView;

@Override
protected void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    MobileAds.initialize(this, "################");

    RelativeLayout layout = new RelativeLayout(this);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);

    AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();

    View gameView = initializeForView(new DoubleDodge(new AndroidDatabaseAccess()), config);

    // Create and setup the AdMob view
    adView = new AdView(this);
    adView.setAdSize(AdSize.SMART_BANNER);
    adView.setAdUnitId("#############");
    adView.setVisibility(AdView.VISIBLE);

    // Create an ad request.
    AdRequest.Builder adRequestBuilder = new AdRequest.Builder();

    // Optionally populate the ad request builder.
    adRequestBuilder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);


    // Add the gameView to the view hierarchy
    layout.addView(gameView);

    // Start loading the ad.
    adView.loadAd(adRequestBuilder.build());


    // Add the AdMob view
    RelativeLayout.LayoutParams adParams =
            new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
    adParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    adParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

    // Add the AdView to the view hierarchy.
    layout.addView(adView, adParams);

    // Hook it all up
    setContentView(layout);

    //initialize(new DoubleDodge(new AndroidDatabaseAccess()), config);
}

@Override
public void onResume() {
    super.onResume();

    // Resume the AdView.
    adView.resume();
}

@Override
public void onPause() {
    // Pause the AdView.
    adView.pause();

    super.onPause();
}

@Override
public void onDestroy() {
    // Destroy the AdView.
    adView.destroy();

    super.onDestroy();
}
}

答案 2 :(得分:0)

实际上我认为你是在思考它:

<html>
  <head>
    <script
  src="https://code.jquery.com/jquery-3.2.1.js"
  integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE="
  crossorigin="anonymous"></script>
  </head>
<body>

         <select name="selection" id="selection" onchange="update(this.value)">
              <option value="1">One</option>
              <option value="2">Two</option>
              <option value="3">Three</option>

          </select>

<input name="desc" type="text" id="desc" />

</body>
<script>
function update(val) {
    $('#desc').val(val);
 }
 </script>
</html>

答案 3 :(得分:0)

您可以使用正确的jQuery代码

i = 0
while (i<= 10):
  print("insert into Member" + "(Mem_ID)")
  print("values" + "(" + "Mem%d" +  ")" %(i))
i = i+1

答案 4 :(得分:0)

您可以对脚本进行两​​处更改。

A)删除$('desc').val(val)行。由于x.value = val就足够了,所以不需要它。

B)您可以将var x = document.getElementsByName("desc")更改为var x = document.getElementById("desc")。由于您的名称和ID相同,我们可以使用getElementById()代替当前的名称。因为getElementsByName()返回一个数组。

所以代码看起来像。

<select name="selection" id="selection" onchange="update(this.value)">
    <option value="1">One</option>
    <option value="2">Two</option>
    <option value="3">Three</option>
</select>

<input name="desc" type="text" id="desc" />

<script>
function update(val) {
    var x = document.getElementById("desc");
    x.value = val;
}
</script>

此外,由于您通过onchange事件调用了javascript方法,因此如果select元素只有1个选项,则您的解决方案将无效。要解决此问题,请尝试使用onblur或onclick取代onchange取决于您的用例。