如何使用javascript创建多值输入文本框?

时间:2016-08-13 11:45:24

标签: javascript html

我想使用java脚本创建如下所示的输入字段。请提供适当的解决方案。



public class MainActivity extends Activity {

TextView textview;
JSONObject json = null;
String str = "";
HttpResponse response;
Context context;
ProgressBar progressbar;
Button button;
JSONArray jArray;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    progressbar = (ProgressBar)findViewById(R.id.progressBar1);
    textview = (TextView)findViewById(R.id.textView1);
    button = (Button)findViewById(R.id.button1);

    progressbar.setVisibility(View.GONE);

    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            progressbar.setVisibility(View.VISIBLE);

            new GetTextViewData(context).execute();

        }
    });
}

public static Map<String,String> parse(JSONObject json , Map<String,String> out) throws JSONException{
    Iterator<String> keys = json.keys();
    while(keys.hasNext()){
        String key = keys.next();
        String val = null;
        try{
            JSONObject value = json.getJSONObject(key);
            parse(value,out);
        }catch(Exception e){
            val = json.getString(key);
        }

        if(val != null){
            out.put(key,val);
        }
    }
    return out;
}

private class GetTextViewData extends AsyncTask<Void, Void, Void>
{
    public Context context;

    public GetTextViewData(Context context)
    {
        this.context = context;
    }

    @Override
    protected void onPreExecute()
    {
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(Void... arg0)
    {

        HttpClient myClient = new DefaultHttpClient();
        HttpPost myConnection = new HttpPost("http://192.168.1.9:80/test-androidex/send-data.php");

        try {
            response = myClient.execute(myConnection);
            str = EntityUtils.toString(response.getEntity(), "UTF-8");

        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


        try{
            JSONArray jArray = new JSONArray(str);
            json = jArray.getJSONObject(0);

        } catch ( JSONException e) {
            e.printStackTrace();
        }

        catch (Exception e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
    protected void onPostExecute(Void result)
    {

     try {
           textview.setText(json.getString("name"));

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        progressbar.setVisibility(View.GONE);

    }
}
&#13;
&#13;
&#13;

元素已创建,但当我发布表单时,我没有获得任何值。我用过这段代码。

&#13;
&#13;
           <input type="text" name="package_location[]"  class="form-control" >
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:1)

你可以创建输入元素,但你必须使用jquery追加到body中

Option Explicit

Private WithEvents IE As InternetExplorer

Private Sub IE_OnQuit()

End Sub

Private Sub IE_OnVisible(ByVal Visible As Boolean)

End Sub

Private Sub Class_Initialize()
    Set IE = New InternetExplorer
End Sub

答案 1 :(得分:0)

我的java脚本代码是正确的只有错误是我创建了输入字段名称,如package_location []。并将其作为Package_location获取大写字母P和小p的差异,即getcha。

答案 2 :(得分:0)

您的代码存在的问题是您忘记将元素附加到DOM。使用纯JavaScript,只需使用您制作的元素并将其附加到正文:

var parinput = document.createElement('input'); //Create element
parinput.type = "text"; //Add attribute "text"
parinput.name = "package_location[]"; //Add attribute "name"
document.getElementsByTagName("body")[0].appendChild(parinput) //Append to the first body element found

此解决方案无需JQuery即可运行。