更改没有id的输入字段的值

时间:2016-02-28 14:20:03

标签: javascript

有人可以帮我这个吗? 如何在没有ID的情况下更改输入字段的值? 我无法更改输入字段,因为我正在与平台集成,它们只能支持js。好的,第一个代码用于读取URL参数。 基本上,它会将值自动填充到表单中。

由于



<html>
<head>
    <script>
        var get_params = function(search_string) {

            var parse = function(params, pairs) {
                var pair = pairs[0];
                var parts = pair.split('=');
                var key = decodeURIComponent(parts[0]);
                var value = decodeURIComponent(parts.slice(1).join('='));

                // Handle multiple parameters of the same name
                if (typeof params[key] === "undefined") {
                    params[key] = value;
                } else {
                    params[key] = [].concat(params[key], value);
                }

                return pairs.length == 1 ? params : parse(params, pairs.slice(1))
            }

            // Get rid of leading ?
            return search_string.length == 0 ? {} : parse({}, search_string.substr(1).split('&'));
        }

        var params = get_params(location.search);

    </script>


</head>
<body>


<!-- nanacast form -->
<tbody>
    <tr id="customFieldsTR01">
        <td id="customFieldsTR01td1">           
            <span style="font-weight: bold;" id="customFieldsTR01td1SPAN1">First Name&nbsp;</span>
        </td>
        <td id="customFieldsTR02td1">
            <input type="text" name="u_firstname" value="">
            <span id="*" class="tableRequired" style="font-weight: bold; color: red;">*</span>
        </td>
    </tr>
    <tr id="customFieldsTR11">
        <td id="customFieldsTR11td1">
            <span style="font-weight: bold;" id="customFieldsTR11td1SPAN1">Last Name&nbsp;</span>
        </td>						<td id="customFieldsTR12td1">
            <input type="text" name="u_lastname" value="">
        </td>
    </tr>
    <tr id="customFieldsTR21">
        <td id="customFieldsTR21td1">
            <span style="font-weight: bold;" id="customFieldsTR21td1SPAN1">Email&nbsp;</span>
        </td>
        <td id="customFieldsTR22td1">
            <input type="text" name="u_email" value="">
            <span id="*" class="tableRequired" style="font-weight: bold; color: red;">*</span>
        </td>
    </tr>

</body>

    <!-- not working since input fields do not have ids
    <script>
        document.getElementById("first_name").value = params['FirstName'];
        document.getElementById("last_name").value = params['LastName'];
        document.getElementById("email").value = params['EMail'];
    </script>
    -------->

</html>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

您可以使用PassengerCompartment并按名称查找它们:

document.querySelector()

答案 1 :(得分:-1)

由于代码中的所有元素都有名称try:

#include <iostream>
#include <vector>

using namespace std;
long long int acc = 0;
class Point
{
    public:
    int row;
    int column;
    int count;
    int map[5][5];
};

vector<Point> pts;
bool isValid(Point *b)
{
    if(b->column > -1 && b->column <5 && b->row > -1 && b->row < 5 && b->map[b->column][b->row] != 1)
{
    return true;
}
else
{
    return false;
}
}
void start(vector<Point> A)
{
if(A.size() == 0)
{
    cout<<acc;
}
Point *temp = &A.back();
A.pop_back();
if(isValid(temp))
{
    temp->map[temp->column][temp->row] = 1;
    temp->count = temp->count + 1;
    if(temp->count == 25)
    {
        acc++;
    }
    else
    {
        Point *p = new Point;
        for(int i = 0; i < 5; i++)
        {
            for(int j = 0;j < 5; j++)
            {
                p->map[i][j] = temp->map[i][j];
            }
        }
        p->column = temp->column + 1;
        p->row = temp->row;
        p->count = temp->count;
        A.push_back(*p);

        Point *q = new Point;
        for(int i = 0; i < 5; i++)
        {
            for(int j = 0;j < 5; j++)
            {
                q->map[i][j] = temp->map[i][j];
            }
        }
        q->column = temp->column - 1;
        q->row = temp->row;
        q->count = temp->count;
        A.push_back(*q);

        Point *r = new Point;
        for(int i = 0; i < 5; i++)
        {
            for(int j = 0;j < 5; j++)
            {
                r->map[i][j] = temp->map[i][j];
            }
        }
        r->column = temp->column;
        r->row = temp->row + 1;
        r->count = temp->count;
        A.push_back(*r);

        Point *s = new Point;
        for(int i = 0; i < 5; i++)
        {
            for(int j = 0;j < 5; j++)
            {
                s->map[i][j] = temp->map[i][j];
            }
        }
        s->column = temp->column;
        s->row = temp->row - 1;
        s->count = temp->count;
        A.push_back(*s);

        Point *t = new Point;
        for(int i = 0; i < 5; i++)
        {
            for(int j = 0;j < 5; j++)
            {
                t->map[i][j] = temp->map[i][j];
            }
        }
        t->column = temp->column + 1;
        t->row = temp->row + 1;
        t->count = temp->count;
        A.push_back(*t);

        Point *u = new Point;
        for(int i = 0; i < 5; i++)
        {
            for(int j = 0;j < 5; j++)
            {
                u->map[i][j] = temp->map[i][j];
            }
        }
        u->column = temp->column - 1;
        u->row = temp->row - 1;
        u->count = temp->count;
        A.push_back(*u);

        Point *v = new Point;
        for(int i = 0; i < 5; i++)
        {
            for(int j = 0;j < 5; j++)
            {
                v->map[i][j] = temp->map[i][j];
            }
        }
        v->column = temp->column + 1;
        v->row = temp->row - 1;
        v->count = temp->count;
        A.push_back(*v);

        Point *w = new Point;
        for(int i = 0; i < 5; i++)
        {
            for(int j = 0;j < 5; j++)
            {
                w->map[i][j] = temp->map[i][j];
            }
        }
        w->column = temp->column - 1;
        w->row = temp->row + 1;
        w->count = temp->count;
        A.push_back(*w);    
    }
}
cout<<A.size();
start(A);
}

int main()
{
pts.clear();
Point *p = new Point;
p->map[2][2] = 0;
p->column = 2;
p->row = 2;
p->count = 0;
cout<<p<<endl;
pts.push_back(*p);
start(pts);

}